package com.jinjiafeng.baselibrary.widget.indicator;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * @author jinjiafeng
 * Time :18-6-3
 */
public class IndicatorContainerView extends FrameLayout {

    private LinearLayout mIndicatorContainer;
    private View mBottomTrackView;
    private int mItemWidth;
    private int mInitTrackMargin;

    public IndicatorContainerView(@NonNull Context context) {
        this(context,null);
    }

    public IndicatorContainerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public IndicatorContainerView(@NonNull Context context, @Nullable AttributeSet attrs, int
            defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mIndicatorContainer = new LinearLayout(context);
        addView(mIndicatorContainer);
    }

    public View getItemViewAt(int index) {
        return mIndicatorContainer.getChildAt(index);
    }

    public void addItemView(View view) {
        mIndicatorContainer.addView(view);
    }

    public void addBottomTrackView(View bottomTrackView,int itemWidth) {
        if(bottomTrackView == null){
            return;
        }
        this.mBottomTrackView = bottomTrackView;
        this .mItemWidth = itemWidth;
        addView(bottomTrackView);
        LayoutParams params = (LayoutParams) bottomTrackView.getLayoutParams();
        params.gravity = Gravity.BOTTOM;
        int mTrackWidth = params.width;
        if(params.width == MATCH_PARENT){
            mTrackWidth = mItemWidth;
        }
        if(params.width>mItemWidth){
            mTrackWidth = mItemWidth;
        }
        params.width = mTrackWidth;
        mInitTrackMargin = (mItemWidth - mTrackWidth) / 2;
        mBottomTrackView.setTranslationX(mInitTrackMargin);
        mBottomTrackView.setLayoutParams(params);

    }

    public void scrollBottomTrack(int position, float positionOffset) {
        float leftMargin = (position + positionOffset) * mItemWidth+mInitTrackMargin;
        mBottomTrackView.setTranslationX(leftMargin);
    }

    public void smoothScrollBottomTrack(int position) {
        final int finalDistance = position * mItemWidth+mInitTrackMargin;
        final float currentDistance = mBottomTrackView.getTranslationX();
        final int distance = (int) (finalDistance - currentDistance);
        final ObjectAnimator animator = ObjectAnimator.ofFloat(mBottomTrackView, "translationX",
                currentDistance,finalDistance);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration((long) (Math.abs(distance) * 0.2f));
        animator.start();
    }
}
